package com.codi.bus.core.dao.impl;

import com.codi.base.dao.PageImpl;
import com.codi.base.dao.v2.GeneratedBaseDaoImpl;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.domain.OrganProfile;
import com.github.pagehelper.PageRowBounds;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 机构DAO impl
 *
 * @author shi.pengyan
 * @version 1.0 2017-09-01 9:12
 * @since 1.0
 */
@Slf4j
@Repository("organProfileDao")
public class OrganProfileDaoImpl extends GeneratedBaseDaoImpl<OrganProfile> implements OrganProfileDao {
    @Override
    public OrganProfile qryByUserId(Long userId) {
        return getSqlSession().selectOne(generateStatement("qryByUserId"), userId);
    }

    @Override
    public OrganProfile qryByProfileId(Long profileId) {
        return getSqlSession().selectOne(generateStatement("qryByProfileId"), profileId);
    }

    @Override
    public OrganProfile queryByAgentId(Long agentId) {
        return getSqlSession().selectOne(generateStatement("queryByAgentId"), agentId);
    }

    @Override
    public int updateTestExpireDate(OrganProfile record) {
        return getSqlSession().update(generateStatement("updateTestExpireDate"), record);
    }

    @Override
    public List<OrganProfile> queryListByUserId(Long userId) {
        return getSqlSession().selectList(generateStatement("queryListByUserId"), userId);
    }

    @Override
    public List<OrganProfile> queryListByAgentId(Long agentId) {
        return getSqlSession().selectList(generateStatement("queryListByAgentId"), agentId);
    }

    @Override
    public Page<OrganProfile> query(OrganProfile record, Pageable pageable) {

        PageRowBounds      prb    = new PageRowBounds(pageable.getPageNumber(), pageable.getPageSize());
        List<OrganProfile> result = getSqlSession().selectList(generateStatement("query"), record, prb);


        return new PageImpl<>(result, pageable, prb.getTotal());
    }

    @Override
    public Boolean checkBusinessLicenseExist(String bizLicense) {
        OrganProfile record = new OrganProfile();
        record.setBusinessLicence(bizLicense);

        return getSqlSession().selectOne(generateStatement("checkBusinessLicense"), record);
    }

    @Override
    public Boolean checkBusinessLicenseExist(String bizLicense, Long profileId) {
        OrganProfile record = new OrganProfile();
        record.setBusinessLicence(bizLicense);
        record.setProfileId(profileId);

        return getSqlSession().selectOne(generateStatement("checkBusinessLicenseExceptSelf"), record);
    }

    @Override
    public List<OrganProfile> queryAllOrganProfiles() {
        return getSqlSession().selectList(generateStatement("queryAllOrganProfiles"), null);
    }
}
